package com.sofwin.yyb.controller;

import com.sofwin.yyb.dto.AppointmentDto;
import com.sofwin.yyb.dto.CourseDto;
import com.sofwin.yyb.dto.MechanismDto;
import com.sofwin.yyb.entity.Admin;
import com.sofwin.yyb.entity.MyPage;
import com.sofwin.yyb.entity.User;
import com.sofwin.yyb.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manage")
public class AdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    UserService userService;
    @Autowired
    MechanismService mechanismService;
    @Autowired
    AppointmentService appointmentService;
    @Autowired
    CourseService courseService;

    @RequestMapping("")
    public String visit(){
        return "redirect:/manage/page/login/login.html";
    }

    /**
     * 后台管理登录
     * @param administer
     * @param session
     * @param code
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public Map<String,Object> toLogin(Admin administer, HttpSession session,String code){
        Map<String,Object> map = new HashMap<>();
        String yzm = (String)session.getAttribute("code");
        if(yzm.equals(code)){
            Admin admin = adminService.findAdminister(administer);
            if(admin != null){
                session.setAttribute("admin",admin);
                map.put("msg",1);  //用户名密码输入正确，登录成功
            }else{
                map.put("msg",2);   //用户名密码输入错误，请重新登录
            }
        }else{
            map.put("msg",3);  //验证码输入有误
        }
        return map;
    }

    /**
     * 用户列表加载数据
     * @param pageNumber
     * @param pageSize
     * @param search
     * @return
     */
    @RequestMapping("/userList")
    @ResponseBody
    public Map<String,Object> findAllUser(
            @RequestParam(defaultValue = "0") Integer pageNumber,
            @RequestParam(defaultValue = "2") Integer pageSize,
            @RequestParam(required = false) String search){
        Map<String,Object> map = new HashMap<>();
        map.put("pageNumber",pageNumber);
        map.put("pageSize",pageSize);
        map.put("search",search);
        //查询对象返回集合
        List<User> userList = userService.findAllUserByPage(map);
        //查询数据条数
        Integer count = userService.userCount(search);
        MyPage page = new MyPage();
        page.setDataList(userList);
        page.setPageNumber(pageNumber);
        page.setPageSize(pageSize);
        page.setTotal(count);
        Map<String,Object> pageInfo = new HashMap<>();
        pageInfo.put("page",page);
        pageInfo.put("search",search);
        return pageInfo;
    }

    /**
     * 机构列表加载数据
     * @param pageNumber
     * @param pageSize
     * @param search
     * @return
     */
    @RequestMapping("/mechanismList")
    @ResponseBody
    public Map<String,Object> findAllMechanism(
            @RequestParam(defaultValue = "0") Integer pageNumber,
            @RequestParam(defaultValue = "2") Integer pageSize,
            @RequestParam(required = false) String search){
        Map<String,Object> map = new HashMap<>();
        map.put("pageNumber",pageNumber);
        map.put("pageSize",pageSize);
        map.put("search",search);
        List<MechanismDto> list =  mechanismService.findAllMechanism(map);
        Integer count = mechanismService.mechanismCount(search);
        MyPage page = new MyPage();
        page.setDataList(list);
        page.setPageNumber(pageNumber);
        page.setPageSize(pageSize);
        page.setTotal(count);
        Map<String,Object> pageInfo = new HashMap<>();
        pageInfo.put("page",page);
        pageInfo.put("search",search);
        return pageInfo;
    }

    /**
     * 预约信息页面加载数据
     * @param pageNumber
     * @param pageSize
     * @param search
     * @return
     */
    @RequestMapping("/appointmentList")
    @ResponseBody
    public Map<String,Object> findAllAppointment(
            @RequestParam(defaultValue = "0") Integer pageNumber,
            @RequestParam(defaultValue = "2") Integer pageSize,
            @RequestParam(required = false) String search){
        Map<String,Object> map = new HashMap<>();
        map.put("pageNumber",pageNumber);
        map.put("pageSize",pageSize);
        map.put("search",search);
        List<AppointmentDto> list = appointmentService.findAllAppointment(map);
        Integer count = appointmentService.appointmentCount(search);
        MyPage page = new MyPage();
        page.setPageSize(pageSize);
        page.setPageNumber(pageNumber);
        page.setTotal(count);
        page.setDataList(list);
        Map<String,Object> pageInfo = new HashMap<>();
        pageInfo.put("page",page);
        pageInfo.put("search",search);
        return pageInfo;
    }

    /**
     * 课程列表页面数据加载
     * @param pageNumber
     * @param pageSize
     * @param search
     * @return
     */
    @RequestMapping("/courseList")
    @ResponseBody
    public Map<String,Object> findAllCourse(
            @RequestParam(defaultValue = "0") Integer pageNumber,
            @RequestParam(defaultValue = "2") Integer pageSize,
            @RequestParam(required = false) String search){
         Map<String,Object> map = new HashMap<>();
         map.put("pageNumber",pageNumber);
         map.put("pageSize",pageSize);
         map.put("search",search);
         List<CourseDto> list = courseService.findAllCourse(map);
         Integer count = courseService.courseCount(search);
         MyPage page = new MyPage();
         page.setTotal(count);
         page.setPageNumber(pageNumber);
         page.setPageSize(pageSize);
         page.setDataList(list);
         Map<String,Object> pageInfo = new HashMap<>();
         pageInfo.put("page",page);
         pageInfo.put("search",search);
         return pageInfo;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping("/delUser")
    @ResponseBody
    public Map<String,Object> delUser(Integer id){
        Map<String,Object> map = new HashMap<>();
        boolean flag = userService.deleteById(id);
        System.out.println(flag);
        if(flag){
            map.put("msg",1);  //删除成功
        }else{
            map.put("msg",2);  //删除失败
        }
        return map;
    }

    @RequestMapping("/findUser")
    @ResponseBody
    public User findUser(Integer id){
        return userService.findById(id);
    }

    /**
     * 审核课程
     * @return
     */
    @RequestMapping("/updateStatus")
    @ResponseBody
    public boolean updateCourseStatus(
            @RequestParam Integer code,
            @RequestParam Integer id){
        Map<String,Object> map = new HashMap<>();
        map.put("code",code);
        map.put("id",id);
        return courseService.updateCourseStatus(map);
    }
}
